<%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2020/10/26
  Time: 14:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>员工列表</title>
    <%
        pageContext.setAttribute("APP_PATH",request.getContextPath());
    %>
    <script  src="${APP_PATH}/static/js/jquery-3.1.1.js"></script>
    <link href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    <script  src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
<%--注册模态框--%>

<%--登录模态框--%>
<div class="modal fade" tabindex="-1" role="dialog" id="empAddModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加员工</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">empName</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="empName" id="empName_add_input" placeholder="empName" autoComplete="off">
                            <span class="help-block"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">email</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="email" id="email_add_input" placeholder="email@qq.com" autocomplete="off">
                            <span class="help-block"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">gender</label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="gender1_add_input" value="M" checked="checked"> 男
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="gender2_add_input" value="F"> 女
                        </label>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">deptName</label>
                        <div class="col-sm-4">
                            <select class="form-control" name="dId" id="dept_select">
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="login_save_btn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<%--修改模态框--%>
<div class="modal fade" tabindex="-1" role="dialog" id="empUpdateModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">修改员工</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">empName</label>
                        <div class="col-sm-10">
                            <p class="form-control-static" name="empName" id="empName_update_static"> </p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">email</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="email" id="email_update_input" placeholder="email@qq.com" autocomplete="off">
                            <span class="help-block"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">gender</label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="gender1_update_input" value="M" checked="checked"> 男
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="gender2_update_input" value="F"> 女
                        </label>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">deptName</label>
                        <div class="col-sm-4">
                            <select class="form-control" name="dId" id="dept_update_select">
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="emp_update_btn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<%--新增模态框--%>
<div class="modal fade" tabindex="-1" role="dialog" id="empAddModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加员工</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">empName</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="empName" id="empName_add_input" placeholder="empName" autoComplete="off">
                            <span class="help-block"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-2 control-label">email</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="email" id="email_add_input" placeholder="email@qq.com" autocomplete="off">
                            <span class="help-block"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">gender</label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="gender1_add_input" value="M" checked="checked"> 男
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="gender" id="gender2_add_input" value="F"> 女
                        </label>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">deptName</label>
                        <div class="col-sm-4">
                        <select class="form-control" name="dId" id="dept_select">
                        </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="emp_save_btn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<%--搭建显示页面--%>
<div class="container">
    <%--    标题--%>
    <div class="row">
        <div class="col-md-12">
            <h1>员工管理-CRUD</h1>
        </div>
    </div>
        <div class="row">
            <div class="col-md-4 col-md-offset-10">
                <button class="btn btn-warning" id="login_btn" >注册</button>
                <button class="btn btn-info " id="regist_btn">登录</button>
            </div>
        </div>
    <div class="row">
        <%--    ------------查找框--------------   --%>
        <div class="col-lg-4">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="输入员工id或员工姓名" id="search_emp">
                <span class="input-group-btn">
                        <%-- 查找按钮 --%>
                        <button class="btn btn-primary glyphicon glyphicon-search" type="button" id="search_btn">Search</button>
                    </span>
            </div><!-- /input-group -->
        </div><!-- /.col-lg-6 -->

            <%--    新增/删除 按钮--%>
        <div class="col-md-4 col-md-offset-10">
            <button class="btn btn-info" id="emp_add_modal_btn" >新增</button>
            <button class="btn btn-danger" id="emp_delete_batch_btn">删除</button>
        </div>
    </div>

    <%--    显示表格数据--%>
    <div class="row">
        <div class="col-md-12">
            <table class="table table-hover" id="emps_table">
                <%-- 表头 --%>
                <thead>
                <tr>
                    <th><input type="checkbox" id="check_all" /></th>
                    <th>#</th>
                    <th>empName</th>
                    <th>gender</th>
                    <th>email</th>
                    <th>deptName</th>
                    <th>操作</th>
                </tr>
                </thead>

                <tbody><%-- 员工数据放在tbody里 --%></tbody>

            </table>
        </div>
    </div>

    <%--    显示分页信息--%>
    <div class="row">
        <%--            分页文字信息--%>
        <div class="col-md-6" id="page_info_area">

        </div>

        <%--            分页条信息--%>
        <div class="col-md-6" id="page_nav_area">

        </div>
    </div>
</div>

<%--                            js部分                                     --%>

<script type="text/javascript">
    //总记录数
    var totalRecordCount;
    //当前页
    var currenpage;

   $(function () {
       to_page(1);
   });

   function to_page(pn) {
       $.ajax({
           url:"${APP_PATH}/emps",
           data:"pn="+pn,
           type:"get",
           success:function (result) {
               build_emps_table(result);
               build_page_info(result);
                build_page_nav(result);
           }
       });
   }
   function build_emps_table(result) {
       //清空table表格，如果不清空，当页面刷新的时候新的数据不会覆盖旧数据，造成页面混乱
       $("#emps_table tbody").empty();
       //获取所有员工数据
       var emps = result.extend.pageInfo.list;

       //遍历员工数据
       $.each(emps, function (index, item) {
           var checkTd=$("<td><input type='checkbox' class='check_item'/></td>").append("")
           var empIdTd = $("<td></td>").append(item.empId);
           var empNameTd = $("<td></td>").append(item.empName);
           var genderTd = $("<td></td>").append(item.gender == 'M' ? "男" : "女");
           var emailTd = $("<td></td>").append(item.email);
           var deptNameTd = $("<td></td>").append(item.department.deptName);

           //编辑按钮
           var editBtn = $("<button></button>").addClass("btn btn-info btn-sm edit_btn")
               .append($("<span></span>").addClass("glyphicon glyphicon-pencil").append("编辑"));
           //为编辑按钮添加一个自定义的属性，来表示当前员工id
           editBtn.attr("edit-id",item.empId);

           //删除按钮
           var delBtn = $("<button></button>").addClass("btn btn-danger btn-sm del_btn")
               .append($("<span></span>").addClass("glyphicon glyphicon-trash").append("删除"));
           //为删除按钮添加一个自定义的属性，来表示当前员工id
           delBtn.attr("del-id",item.empId);

           //把两个按钮放到一个单元格中，并且按钮之间留点空隙
           var btnTd = $("<td></td>").append(editBtn).append(" ").append(delBtn);

           //append方法执行完成以后还是会返回原来的元素，所以可以一直.append添加元素，
           //将上面的td添加到同一个tr里
           $("<tr></tr>").append(checkTd)
               .append(empIdTd)
               .append(empNameTd)
               .append(genderTd)
               .append(emailTd)
               .append(deptNameTd)
               .append(btnTd)
               .appendTo("#emps_table tbody");//将tr添加到tbody标签中
       });

   }

   function build_page_info(result) {
       $("#page_info_area").empty();
       $("#page_info_area").append("<h5>当前第<span class='label label-default'>"+ result.extend.pageInfo.pageNum +"</span>页，总<span class='label label-default'>"+ result.extend.pageInfo.pages+ "</span>页,共<span class='label label-default'>"+result.extend.pageInfo.total+"</span>条记录。</h5>")

       totalRecordCount=result.extend.pageInfo.total;
        currenpage=result.extend.pageInfo.pageNum;

   }
   function build_page_nav(result) {
       //清空分页条，如果不清空，当页面刷新的时候新的数据不会覆盖旧数据，造成页面混乱
       $("#page_nav_area").empty();

       var ul=$("<ul></ul>").addClass("pagination");

        var firstPageLi=$("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
        var prePageLi=$("<li></li>").append($("<a></a>").append("&laquo;"));
        if (result.extend.pageInfo.hasPreviousPage==false){
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");
        }else {
            firstPageLi.click(function () {
                to_page(1);
            });
            prePageLi.click(function () {
                to_page(result.extend.pageInfo.pageNum-1);
            });
        }



       //下一页和尾页
       var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
       var lastPageLi = $("<li></li>").append($("<a></a>").append("尾页").attr("href","#"));
       if (result.extend.pageInfo.hasNextPage==false){
           nextPageLi.addClass("disabled");
           lastPageLi.addClass("disabled");
       }else {
           lastPageLi.click(function () {
               to_page(result.extend.pageInfo.pages);
           });
           nextPageLi.click(function () {
               to_page(result.extend.pageInfo.pageNum+1);
           });
       }
       ul.append(firstPageLi).append(prePageLi);

       $.each(result.extend.pageInfo.navigatepageNums,function (index,item) {
            var numLi=$("<li></li>").append($("<a></a>").append(item));
            numLi.click(function () {
                to_page(item);
            })
           if (result.extend.pageInfo.pageNum==item){
               numLi.addClass("active");
           }
            ul.append(numLi);
       })
       ul.append(nextPageLi).append(lastPageLi);
       var navEle=$("<nav></nav>").append(ul).appendTo("#page_nav_area");
   }
   //=====================================添加功能

    //=========清空表单样式及内容

    function reset_form(ele){
        //清空表单内容，取出dom对象，调用reset()方法
       $(ele)[0].reset();
        //清空表单样式
       $(ele).find("*").removeClass("has-error has-success");
        //清空提示信息
        $(ele).find(".help-block").text("");
    }

//================弹出添加模态框
  $("#emp_add_modal_btn").click(function () {

      reset_form("#empAddModal form");

        //发送ajax请求，在弹出模态框之前查询部门信息，显示在下拉列表中
      getDepts("#empAddModal select");

      $("#empAddModal").modal({
          backdrop:"static"
      })
  })
//========查询所有部门名
    function getDepts(ele) {
        $(ele).empty();
        $.ajax({
            url:"${APP_PATH}/depts",
            type: "get",
            success:function (result) {
                $.each(result.extend.depts,function () {
                    var optionEle=$("<option></option>").append(this.deptName).attr("value",this.deptId);
                    optionEle.appendTo(ele);
                })
            }
        })
    }

    //这里将校验结果的提示信息全部抽取出来
    function show_validate_msg(ele,status,msg) {
        $(ele).parent().removeClass("has-success has-error");
        $(ele).next("span").text("");

        if ("success"==status){
            //校验成功
            $(ele).parent().addClass("has-success");
            $(ele).next("span").text(msg);
        }else if ("error"==status){
            $(ele).parent().addClass("has-error");
            $(ele).next("span").text(msg);
        }
    }
    //校验方法，判断用户名是否重复或者不可用
    function validate_add_form() {
        var empName=$("#empName_add_input").val();
        var  regName=/(^[a-zA-Z0-9_-]{6,16}$)|(^[\u2E80-\u9FFF]{2,5})/;
        if (!regName.test(empName)){
            //失败
            show_validate_msg("#empName_add_input","error","用户名必须是6-16位数字，字母或者_-，也可以是2-5位中文组成");
            return false;
        }else {
            //成功
            show_validate_msg("#empName_add_input","success","");
        }

        var email=$("#email_add_input").val();
        var regEmaile=/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
        if (!regEmaile.test(email)){
            show_validate_msg("#email_add_input","error","邮箱格式不正确!");
            return false;
        }else{
            show_validate_msg("#email_add_input","success","");
        }
        return true;
    }

   //设置email的校验状态
    var emailStatus;
   $("#email_add_input").change(function () {
        if (!validate_add_form()){
            show_validate_msg("#email_add_input", "error", "邮箱格式错误");
            emailStatus=false;
        }else {
            show_validate_msg("#email_add_input", "success","");
            emailStatus = true;
        }
   })

    //校验用户名是否可用
    $("#empName_add_input").change(function () {
        var empName=this.value;

        $.ajax({
            url:"${APP_PATH}/checkname",
            data:"empName="+empName,
            type:"get",
            success:function (result) {
                if (result.code==100){
                    show_validate_msg("#empName_add_input","success","用户名可用");
                    $("#emp_save_btn").attr("ajax-value", "success");
                }else {
                    show_validate_msg("#empName_add_input","error",result.extend.va_msg);
                    //    如果用户名不可用，将error标志保存到ajax-value属性中
                    $("#emp_save_btn").attr("ajax-value", "error");
                }
            }
        })
    })

   //=========保存新增员工信息
   $("#emp_save_btn").click(function () {
       if ($(this).attr("ajax-value")=="error"){
           return false;
       }

       if (!validate_add_form()|| !emailStatus){
           return false;
       }

       $.ajax({
           url:"${APP_PATH}/emp",
           type:"post",
           data:$("#empAddModal form").serialize(),
           success:function (result) {
                if (result.code==100){
                    //保存成功
                    //  1、关闭模态框
                    $('#empAddModal').modal('hide');
                    //2、跳转到最后一页
                    to_page(totalRecordCount);
                }else {
                    //邮箱有错
                    if (result.extend.errorField.email!=undefined){
                        show_validate_msg("#email_add_input","error",result.extend.errorField.email);
                    }
                    if (result.extend.errorField.empName!=undefined){
                        show_validate_msg("#empName_add_input","error",result.extend.errorField.empName);
                    }
                }
           }
       });
   });
//==============================修改


    $(document).on("click",".edit_btn",function () {

        //获取部门
        getDepts("#empUpdateModal select");
        //根据id获取回显信息
        getEmp($(this).attr("edit-id"));
        $("#emp_update_btn").attr("edit-id",$(this).attr("edit-id"));
        $("#empUpdateModal").modal({
            backdrop: "static"
        });
    })

    function getEmp(id) {
        $.ajax({
            url:"${APP_PATH}/emp/"+id,
            type:"GET",
            success:function (result) {
                var empDate=result.extend.emp;
                $("#empName_update_static").text(empDate.empName);
                $("#email_update_input").val(empDate.email);
                //注意中括号
                $("#empUpdateModal input[name=gender]").val([empDate.gender]);
                $("#empUpdateModal select").val([empDate.dId]);
            }
        })
    }

    $("#email_update_input").change(function () {
        validate_update_email();
    })
    function validate_update_email(){
        var email=$("#email_update_input").val();
        var regEmaile=/^\w+@[a-z0-9]+\.[a-z]{2,4}$/;
        if (!regEmaile.test(email)){
            show_validate_msg("#email_add_input","error","邮箱格式不正确!");
            return false;
        }else{
            show_validate_msg("#email_add_input","success","");
        }
        return true;
    }

    $("#emp_update_btn").click(function () {
        //验证邮箱是否合法
        var email = $("#email_update_input").val();
        var regEmail = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
        if(!regEmail.test(email)){
            show_validate_msg("#email_update_input", "error", "邮箱格式不正确!");
            return false;
        }else{
            show_validate_msg("#email_update_input", "success", "");
        }
        var empid=$(this).attr("edit-id");
        $.ajax({
            url:"${APP_PATH}/emp/"+empid,
            data:$("#empUpdateModal form").serialize(),
            //在ajax可直接用put和delete请求，但tomcat不会自动装配getParamters的model
            //，需要设置HttpPutFormContentFilter过滤器
            type:"PUT",
            success:function (result) {
                //关闭模态框
                $("#empUpdateModal").modal("hide");
                //返回当前页
                var searchContent=$("#search_emp").val();
                if (searchContent.length==0){
                    to_page(currenpage);
                }else {
                    search_to_page(currenpage);
                }
            }
        })
    })

    //============删除
    //单个删除
    $(document).on("click",".del_btn",function () {
        //alert($(this).parents("tr").find("td:eq(2)").text())
        var empId=$(this).attr("del-id");
       if (confirm("你确定要删除【"+$(this).parents("tr").find("td:eq(2)").text()+"】吗?")){
           $.ajax({
               url:"${APP_PATH}/emp/"+empId,
               type:"DELETE",
               success:function (result) {
                   alert(result.msg);
                   var searchContent=$("#search_emp").val();
                   if (searchContent.length==0){
                       to_page(currenpage);
                   }else {
                       search_to_page(currenpage);
                   }
               }
           })
       }
    });

    //全选 和  全不选
    $("#check_all").click(function () {
        $(".check_item").prop("checked",$(this).prop("checked"))
    });

    //    当本页面所有复选框都选上时，自动将全选复选框选上
    $(document).on("click",".check_item",function () {
        var flag=$(".check_item:checked").length==$(".check_item").length;
        $("#check_all").prop("checked",flag);
    })

    $("#emp_delete_batch_btn").click(function () {
        var empNames="";
        var empIds="";
        $.each($(".check_item:checked"),function () {
            empNames+=$(this).parents("tr").find("td:eq(2)").text()+",";
            empIds+=$(this).parents("tr").find("td:eq(1)").text()+"-";
        })
        empNames=empNames.substring(0,empNames.length-1);
        if (confirm("你要删除【"+empNames+"】吗?")){
            $.ajax({
                url:"${APP_PATH}/emp/"+empIds,
                type:"DELETE",
                success:function (result) {
                    alert(result.msg);
                    var searchContent=$("#search_emp").val();
                    if (searchContent.length==0){
                        to_page(currenpage);
                    }else {
                        search_to_page(currenpage);
                    }
                }
            })
        }
    })

    //=================模糊查询
    $("#search_btn").click(function () {
        $("#emps_table tbody").empty();

        var searchContent=$("#search_emp").val();


        //如果输入框中的内容为空的话，用to_page回到原本显示的界面。
        if(searchContent == ""){
            to_page(1);
        }else {
            $.ajax({
                url:"${APP_PATH}/empSearch",
                data:"content="+searchContent,
                type:"GET",
                success:function (result) {
                    //显示搜索到的员工
                    search_emps_table(result);
                    //显示搜索页面的分页信息
                    build_search_page_info(result);
                    build_search_page_nav(result)
                }
            })
        }

    })

    function search_emps_table(result) {
        $("#emps_table tbody").empty();
        var searchContent=$("#search_emp").val();
        //将查找出来的员工数据保存在empSearched中
        var empSearched = result.extend.pageInfo.list;

        //如果empSearch中的数据为空的话，就提示未找到
        if(empSearched.length == 0){
            var waring="<div class='col-md-12 alert alert-warning' role='alert' style='text-align: center'>没有找到与["+searchContent+"]相关的员工</div>";
            $("<tr></tr>").append($("<td colspan='12'></td>").append(waring))
                .appendTo("#emps_table tbody");//将tr添加到tbody标签中
            $("#page_nav_area").empty().hide();
        }else {
            build_emps_table(result);
        }

    }

    function build_search_page_info(result) {
        $("#page_info_area").empty();
        $("#page_info_area").append("<h5>已查询到<span class='label label-default'>"+result.extend.pageInfo.total+"</span>条记录,当前第<span class='label label-default'>"+ result.extend.pageInfo.pageNum +"</span>页，总<span class='label label-default'>"+ result.extend.pageInfo.pages+ "</span>页.</h5>");
        currenpage=result.extend.pageInfo.pageNum;
    }

    function search_to_page(pn) {
        var searchContent=$("#search_emp").val();

        $.ajax({
            url:"${APP_PATH}/empSearch",
            data:"content="+searchContent+"&pn="+pn,
            type:"GET",
            success:function (result) {
                //显示搜索到的员工
                search_emps_table(result);
                //显示搜索页面的分页信息
                build_search_page_info(result);
                build_search_page_nav(result)
            }
        });
    }

    function build_search_page_nav(result) {
        $("#page_nav_area").empty();

        var ul=$("<ul></ul>").addClass("pagination");

        var firstPageLi=$("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
        var prePageLi=$("<li></li>").append($("<a></a>").append("&laquo;"));
        if (result.extend.pageInfo.hasPreviousPage==false){
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");
        }else {
            firstPageLi.click(function () {
                search_to_page(1);
            });
            prePageLi.click(function () {
                search_to_page(result.extend.pageInfo.pageNum-1);
            });
        }



        //下一页和尾页
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("尾页").attr("href","#"));
        if (result.extend.pageInfo.hasNextPage==false){
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        }else {
            lastPageLi.click(function () {
                search_to_page(result.extend.pageInfo.pages);
            });
            nextPageLi.click(function () {
                search_to_page(result.extend.pageInfo.pageNum+1);
            });
        }
        ul.append(firstPageLi).append(prePageLi);

        $.each(result.extend.pageInfo.navigatepageNums,function (index,item) {
            var numLi=$("<li></li>").append($("<a></a>").append(item));
            numLi.click(function () {
                search_to_page(item);
            })
            if (result.extend.pageInfo.pageNum==item){
                numLi.addClass("active");
            }
            ul.append(numLi);
        })
        ul.append(nextPageLi).append(lastPageLi);
        var navEle=$("<nav></nav>").append(ul).appendTo("#page_nav_area");
    }

</script>
</body>
</html>
